package com.xiaoxin.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaoxin.admin.exception.BusinessException;
import com.xiaoxin.dubbo.api.MovementApi;
import com.xiaoxin.dubbo.api.UserInfoApi;
import com.xiaoxin.dubbo.api.VideoApi;
import com.xiaoxin.model.domain.UserInfo;
import com.xiaoxin.model.mongo.Movement;
import com.xiaoxin.model.vo.MovementsVo;
import com.xiaoxin.model.vo.MovementsVo2;
import com.xiaoxin.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxiaode
 * @date 2021/9/14 20:35
 **/

@Service
public class ManagerService {


    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    /*
     * 获取用户信息
     * */
    public PageResult getUsers(Integer page, Integer pagesize) {
        IPage users = userInfoApi.getUsers(page, pagesize);
        // total:分页对象记录列表
        return new PageResult(page, pagesize, users.getTotal(), users.getRecords());
    }

    /*
     * 根据id查询
     * */
    public UserInfo findByUserId(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return userInfo;
    }

    /*
     * 根据用户id查询用户视频
     * */
    public PageResult getUserVideos(Integer page, Integer pagesize, Long uid) {
        PageResult result = videoApi.getUserVideos(page, pagesize, uid);
        return result;
    }

    /*
     * 查询用户动态
     * */
    public PageResult getMessages(Integer page, Integer pagesize, Long uid, Integer state) {
        // 查询movement对象
        PageResult result = movementApi.getMessages(uid, state, page, pagesize);
        // 解析pageResult
        List<Movement> movements = (List<Movement>) result.getItems();
        // 获取movementVo
        if (CollUtil.isEmpty(movements)) {
            return new PageResult();
        }
        List<Long> userIds = CollUtil.getFieldValues(movements, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : movements) {
            UserInfo userInfo = map.get(movement.getUserId());
            if (userInfo != null) {
                MovementsVo vo = MovementsVo.init(userInfo, movement);
                vos.add(vo);
            }
        }
        result.setItems(vos);
        return result;
    }

    /*
     * 查询用户动态详情
     * */
    public MovementsVo2 getMovement(Long movementId) {
        MovementsVo2 vo = null;
        try {
            Movement movement = movementApi.findById(movementId.toString());
            Long userId = movement.getUserId();
            UserInfo userInfo = userInfoApi.findById(userId);
            vo = MovementsVo2.init(userInfo, movement);
        } catch (Exception e) {
            throw new BusinessException("出错了,请节哀");
        }
        return vo;
    }
}
